<template>
  <el-dialog title="选择材料" :visible.sync="showDialog" width="70%" class="choiceMaterial">
    <el-cascader
      :options="departOptions"
      v-model="selectDepart"
      clearable
      placeholder="请选择部门"
      style="width: 380px; margin-bottom: 10px;"
      @change="handleChange"
    ></el-cascader>
    <el-table
      ref="multipleTable"
      v-loading="loading"
      v-if="tableData.length"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="材料名称">
        <template slot-scope="scope">{{ scope.row.materialName }}</template>
      </el-table-column>
    </el-table>
    <div v-else class="none-data">
      <span>暂无数据</span>
    </div>
    <div class="fenye">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="size"
        layout="total, sizes, prev, pager, next"
        :total="total"
      ></el-pagination>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="showDialog = false">取 消</el-button>
      <el-button type="primary" @click="submit()">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getMaterialData } from '@/api/situation/index';
import { getDepartTree } from '@/api/onethingManage';
export default {
  name: 'choiceMaterial',
  data() {
    return {
      showDialog: false,
      loading: false,
      tableData: [],
      checkBoxGroup: [],
      thingName: '',
      current: 1, //当前页
      size: 10, //每页的数量
      total: 0,
      departOptions: [],
      selectDepart: '',
      departs: ['']
    };
  },
  methods: {
    handleChange(opt) {
      this.departs = opt;
      if (opt && opt.length !== 0) {
        let len = opt.length;
        this.departs.push(opt[len - 1]);
      }
      this.initPage();
    },
    submit() {
      this.$emit('setChoiceMaterial', this.checkBoxGroup);
      this.showDialog = false;
    },
    handleSelectionChange(checkBoxGroup) {
      this.$emit('setChoiceMaterial', checkBoxGroup);
    },
    handleSizeChange(val) {
      this.current = 1;
      this.size = val;
      this.checkBoxGroup = [];
      this.initPage();
    },
    handleCurrentChange(val) {
      this.checkBoxGroup = [];
      this.current = val;
      this.initPage();
    },
    clearTable() {
      this.$refs.multipleTable && this.$refs.multipleTable.clearSelection();
    },
    initPage() {
      let params = {
        current: this.current,
        size: this.size,
        departId: this.departs[0]
      };
      this.loading = true;
      getMaterialData(params).then((res) => {
        this.loading = false;
        this.tableData = res.data.result.records;
        this.total = res.data.result.total;
      });
    },
    initTreeData(departOptions) {
      departOptions.forEach((e) => {
        this.$set(e, 'label', e.departName);
        this.$set(e, 'value', e.departId);
        if (e.children && e.children.length != 0) {
          this.initTreeData(e.children);
        }
      });
    }
  },
  mounted() {
    this.initPage();
    //获取部门树
    getDepartTree().then((res) => {
      this.departOptions = res.data.result;
      this.initTreeData(this.departOptions);
    });
  }
};
</script>

<style lang="scss" scoped>
.choiceMaterial {
  .checkbox-group-container {
    margin-left: -20px;
  }
  .el-checkbox {
    position: relative;
    width: 100px;
    margin-bottom: 20px;
    margin-right: 0;
    margin-left: 20px;
  }
  .checkbox-label {
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .checkbox-group-container-item {
    width: 70px;
    height: 100px;
    float: left;
    img {
      display: block;
      width: 70px;
      margin: 0 auto 10px;
    }
  }
}

.fenye {
  margin: 20px 0;
  text-align: center;
}
::v-deep .el-dialog__header {
  border-bottom: 1px solid #ddd;
}
</style>
